package dao

import (
	"gin-system/models"
)

func GetPhysical(physicalParam map[string]interface{}) (error, []models.Physical, int64) {
	var physicalData []models.Physical
	page := physicalParam["page"].(int)
	pageSize := physicalParam["limit"].(int)
	searchName := physicalParam["searchName"].(string)
	var total int64
	err := db.Table("physical").Select("id, student_id, student_name, sex,age,height, weight,chest,create_time, update_time").Where("student_name like ?", searchName+"%").Order("id ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&physicalData).Error

	return err, physicalData, total
}

func Del(id int) error {
	err := db.Table("physical").Where("id = ?", id).Delete(&models.Physical{}).Limit(1).Error
	return err
}

func Update(physical models.Physical) error {
	err := db.Table("physical").Select("id",  "student_id", "student_name", "sex", "age", "height", "weight", "chest","create_time","update_time","faculty").Where("id = ?", physical.Id).Updates(&physical).Error
	return err
}

func Add(physical models.Physical) error {
	err := db.Table("physical").Select("id","student_id", "student_name", "sex", "age", "height", "weight", "chest","create_time","update_time","faculty").Create(&physical).Error
	return err
}

func GetPhysicalByPhysical(physical models.Physical) []models.Physical {
	var resultList [] models.Physical
	physicalLit := db.Table("physical")
	if physical.Id != 0 {
		physicalLit.Where("id = ?", physical.Id)
	}
	if physical.Age != 0 {
		physicalLit.Where("age = ?", physical.Age)
	}
	if physical.StudentId != 0 {
		physicalLit.Where("student_id = ?", physical.StudentId)
	}
	if physical.StudentName != ""{
		physicalLit.Where("student_name = ?", physical.StudentName)
	}
	if physical.Faculty != "" {
		physicalLit.Where("faculty = ?", physical.Faculty)
	}
	physicalLit.Find(&resultList)
	return resultList
}
func GetAllStudent() (error, []models.Physical) {
	var physicalData []models.Physical
	err := db.Table("physical").Select("id, student_id, student_name, sex,age,height, weight,chest,create_time, update_time").Find(&physicalData).Error
	return err,physicalData
}
func GetConsecutiveMean() (error, []models.ConsecutiveMean) {
	var consecutiveMean []models.ConsecutiveMean
	err := db.Table("physical").Select("AVG(age) as age , AVG(height) as height, AVG(weight) as weight , AVG(chest) as chest, create_time").Group("create_time").Order("create_time").Find(&consecutiveMean).Error
	return err,consecutiveMean
}

